package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.common.utils.transfer.RandomUtils;
import cn.dansj.common.utils.transfer.StringUtils;
import cn.dansj.common.utils.transfer.Transformation;
import cn.dansj.common.utils.transfer.Verification;
import cn.dansj.handler.InterfaceHandler;

import java.util.Map;

@MarkAsApi
public class EdgeAddComponent extends InterfaceHandler {
    private static final String insert = "insert into x_edge_component(component ,height ,width ,id ,parent_id ,`show` ,title ,url ,prop ) values(?,?,?,?,?,?,?,?,?)";
    private static final String delete = "delete from x_edge_component where id = ?";

    @Override
    public Object execute(Map<String, Object> paramsMap) {
        String component = Transformation.toString(paramsMap.get("component"));
        int height = Transformation.parseInt(paramsMap.get("height"));
        int width = Transformation.parseInt(paramsMap.get("width"));
        String id = Transformation.toString(paramsMap.get("id"));
        String parent_id = Transformation.toString(paramsMap.get("parent_id"));
        int show = Transformation.transBooleanToInt(Transformation.castToBoolean(paramsMap.get("show")));
        String title = Transformation.toString(paramsMap.get("title"));
        String url = Transformation.toString(paramsMap.get("url"));
        String prop = Transformation.toString(paramsMap.get("prop"));
        if (Verification.checkNotNull(id)) {
            jdbcTemplate.update(delete, id);
        } else {
            id = RandomUtils.getUid();
        }

        int update = jdbcTemplate.update(insert, component, height, width, id, parent_id, show, StringUtils.substr(title, 0, 50), url, prop);
        return update > 0 ? id : "";
    }
}